package com.moss.cloud.plugins.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moss.cloud.common.base.utils.PageUtil;
import com.moss.cloud.common.core.exception.SystemErrorType;
import com.moss.cloud.common.core.utils.BooleanHandel;
import com.moss.cloud.plugins.api.model.from.BaseFrom;
import com.moss.cloud.plugins.api.model.from.CodeTemplateFrom;
import com.moss.cloud.plugins.api.model.param.CodeTemplateParam;
import com.moss.cloud.plugins.biz.mapper.CodeTemplateMapper;
import com.moss.cloud.plugins.biz.model.CodeTemplate;
import com.moss.cloud.plugins.biz.service.ICodeTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 代码模板配置业务实现
 *
 * @author 瑾年
 * @date 2024-01-08
 */
@Slf4j
@Service
public class CodeTemplateServiceImpl extends ServiceImpl<CodeTemplateMapper, CodeTemplate> implements ICodeTemplateService {

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Override
    public IPage<CodeTemplate> page(CodeTemplateParam param) {
        Page<CodeTemplate> page = PageUtil.createPage(param.getCurrent(), param.getSize());
        return this.page(page, new QueryWrapper<CodeTemplate>().lambda()
                .like(StringUtils.isNotBlank(param.getTemplateName()), CodeTemplate::getTemplateName, param.getTemplateName())
                .eq(Objects.nonNull(param.getTemplateType()), CodeTemplate::getTemplateType, param.getTemplateType()));

    }

    /**
     * 新增
     *
     * @param from
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(CodeTemplateFrom from) {
        CodeTemplate codeTemplate = new CodeTemplate();
        BeanUtils.copyProperties(from, codeTemplate);
        return this.save(codeTemplate);
    }

    /**
     * 更新
     *
     * @param from
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(CodeTemplateFrom from) {
        BooleanHandel.trueThrow(Objects.isNull(from.getId())).throwMessage(SystemErrorType.PRIMARY_KEY);
        CodeTemplate codeTemplate = this.getById(from.getId());
        BooleanHandel.trueThrow(Objects.isNull(codeTemplate)).throwMessage(SystemErrorType.DATE_EMPTY);
        CodeTemplate model = new CodeTemplate();
        BeanUtils.copyProperties(from, model);
        return this.updateById(model);
    }


    /**
     * 删除
     *
     * @param baseFrom
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean remove(BaseFrom baseFrom) {
        CodeTemplate codeTemplate = this.getById(baseFrom.getId());
        BooleanHandel.trueThrow(Objects.isNull(codeTemplate)).throwMessage(SystemErrorType.DATE_EMPTY);
        return this.removeById(baseFrom.getId());
    }

    /**
     * 详情
     *
     * @param baseFrom
     * @return
     */
    @Override
    public CodeTemplate detail(BaseFrom baseFrom) {
        return this.getById(baseFrom.getId());
    }

    /**
     * 根据模板类型查询模板
     *
     * @param templateType
     * @return
     */
    @Override
    public List<CodeTemplate> codeTemplateList(List<Integer> templateType) {
        return this.list(new QueryWrapper<CodeTemplate>().lambda()
                .in(CodeTemplate::getTemplateType, templateType));
    }


}
